#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main()
{
  //ps -ef | grep pipe
  int pipefd[2];
  int ret=pipe(pipefd);
  if(ret<0)
  {
    perror("pipe error");
    return -1;
  }
  
  int id1=fork();
  if(id1==0)
  {
    //dup2(int oldfd,int newfd);
    dup2(pipefd[1],1);
    //让1号的地址信息指向pipefd[1],相当于操作管道的// 写入端
    execlp("ps","ps","-ef",NULL);
    exit(-1);
  }
  int id2=fork();
  if(id2==0)
  {
    //关闭输入端
    close(pipefd[1]);
    dup2(pipefd[0],0);
    execlp("grep","grep","pipe",NULL);
    exit(-1);
  }
  close(pipefd[1]);
  close(pipefd[0]);
  wait(NULL);
  wait(NULL);
  return 0;
}
